package com.archeocs.figone.gui;

//Author:
//Miłosz Pigłas, Archeocs milosz@archeocs.com
//
//Copyright (c) 2010, Miłosz Pigłas, Archeocs
//
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
//* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the distribution.
//* Neither the name of the [ORGANIZATION] nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
//A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
//EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
//PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
//PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;

import com.archeocs.figone.lib.K;
import com.archeocs.figone.lib.Uklad;

/**
 * Klasa reprezentuje glowne okno aplikacji
 * @author Milosz Piglas
 *
 */
public class Aplikacja extends JFrame {
	
	private static final long serialVersionUID = -2335740082165894643L;


	/**
	 * Model tabeli
	 * @author Milosz Piglas
	 *
	 */
	private class TabModel extends DefaultTableModel
	{

		private static final long serialVersionUID = -5729865722271624782L;

		public TabModel(String[] kolumny)
		{
			super(kolumny,0);
		}
		
		public void dodajWiersz(String wartosc)
		{
			this.addRow(new Object[] {wartosc, new Boolean(false)});
		}
		
		@Override
		public boolean isCellEditable(int row, int col)
		{
			if (col == 0)
				return false;
			return true;
			
		}
		
		public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

	}
	
	
	private JPanel mainPanel;
	private JScrollPane jScrollPane1;
	private JTable tabPliki;
	private JMenuBar menuGlowne;
	private TabModel model;
	private Uklad uklad;
	private File curDir;

	public Aplikacja()
	{
		super("FigOne");
		aplikacjaInit();
		uklad();
	}
	
	/**
	 * Tworzy nowy uklad i ustawia marginesy
	 */
	private void uklad()
	{
		int tloSz = K.dtoi(210 / 25.4 * 90);
		int tloWy = K.dtoi(297 / 25.4 * 90);
		uklad = new Uklad(tloSz,tloWy);
		uklad.setMarginesy(10);
	}
	
	/**
	 * Dodaje rycine do ukladu
	 */
	private void dodajRycine()
	{
		JFileChooser wyborPliku = new JFileChooser();
		String sciezka = null;
		FileNameExtensionFilter filtr = new FileNameExtensionFilter("Plik PNG","png");
		wyborPliku.setFileFilter(filtr);
		wyborPliku.setCurrentDirectory(curDir);
		int ret = wyborPliku.showOpenDialog(null);
		if (ret == JFileChooser.APPROVE_OPTION)
		{
			sciezka = wyborPliku.getSelectedFile().getAbsolutePath();
			curDir = wyborPliku.getCurrentDirectory();
			model.dodajWiersz(sciezka);
			uklad.dodajRyc(sciezka);
		}
	}
	
	/**
	 * Tworzy menu programu
	 */
	private void utworzMenu()
	{
		JMenu operacje = new JMenu("Operacje");
		JMenuItem dodaj = new JMenuItem("Dodaj");
		ActionListener dodaj_click = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				dodajRycine();
			}
		};
		dodaj.addActionListener(dodaj_click);
		dodaj.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0));
		
		JMenuItem usun = new JMenuItem("Usun");
		ActionListener usun_click = new ActionListener() 
		{

			@Override
			public void actionPerformed(ActionEvent arg0) {
				//System.out.println("klik klik");
				// TODO Auto-generated method stub
				int pm = 0;
				while (pm < model.getRowCount())
				{
					tabPliki.editingStopped(null);
					Boolean toDel = (Boolean)model.getValueAt(pm, 1);
					if (toDel)
					{
;						model.removeRow(pm);
						uklad.usunRyc(pm);
					}
					else
						pm++;
				}
				tabPliki.repaint();
			}
			
		};
		usun.addActionListener(usun_click);
		usun.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));
		
		JMenuItem opis = new JMenuItem("Dodaj opis");
		ActionListener opis_click = new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int sm = tabPliki.getSelectedRow();
				if (sm >= 0)
				{
					tabPliki.clearSelection();
					String opis = JOptionPane.showInputDialog(null, "Dodaj opis ryciny", "Opis ryciny", JOptionPane.PLAIN_MESSAGE);
					if (opis != null)
					{
						uklad.dodajOpisRyc(sm, opis);
						tabPliki.setValueAt(opis, sm, 0);
					}
				}
				
			}
			
		};
		opis.addActionListener(opis_click);
		opis.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_DOWN_MASK));
		
		JMenuItem generuj = new JMenuItem("Generuj układ");
		ActionListener generuj_click = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//uklad.nowyUklad();
				String tytul = JOptionPane.showInputDialog("Wprowadź tytuł tablicy");
				if ("".equals(tytul))
					uklad.setTytul(null);
				else if (tytul != null)
					uklad.setTytul(tytul);
				Widok.showWidok(uklad, Uklad.NOWY);
			}
			
		};
		
		generuj.addActionListener(generuj_click);
		generuj.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_DOWN_MASK));
		
		JMenuItem zapamietaj = new JMenuItem("Zapamietane");
		ActionListener zapamietaj_click = new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				Widok.showWidok(uklad, Uklad.ZAPAMIETANY);
			}
			
		};
		zapamietaj.addActionListener(zapamietaj_click);
		zapamietaj.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y,InputEvent.CTRL_DOWN_MASK));
		
		JMenuItem zamknij = new JMenuItem("Zamknij");
		ActionListener zamknij_click = new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dispose();
			}
			
		};
		
		zamknij.addActionListener(zamknij_click);
		zamknij.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_DOWN_MASK));
		
		JMenu pomoc = new JMenu("Pomoc");
		JMenuItem o_programie = new JMenuItem("O programie");
		ActionListener oprogr_click = new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JOptionPane.showMessageDialog(null, "FigOne 1.0. Copyright (c) 2010 Miłosz Pigłas, Archeocs.com");
			}
			
		};
		o_programie.addActionListener(oprogr_click);
		
		JMenuItem licencja = new JMenuItem("Licencja");
		ActionListener licencja_click = new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JOptionPane.showMessageDialog(null, "Program rozpowszechniany na warunkach licencji BSD. Pełny tekst licencji: " +
						"http://www.opensource.org/licenses/bsd-license.php");
			}
			
		};
		licencja.addActionListener(licencja_click);
		
		
		operacje.add(dodaj);
		operacje.add(opis);
		operacje.add(usun);
		operacje.add(generuj);
		operacje.add(zapamietaj);
		operacje.add(zamknij);
		menuGlowne.add(operacje);
		
		pomoc.add(o_programie);
		pomoc.add(licencja);
		menuGlowne.add(pomoc);
		
		
	}
	
	
	
	private void aplikacjaInit() {

        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabPliki = new javax.swing.JTable();
        menuGlowne = new javax.swing.JMenuBar();
        model = new TabModel(new String[] {"Plik","Usuń"});
        tabPliki.setModel (model);
        tabPliki.getColumnModel().getColumn(1).setMaxWidth(60);
        jScrollPane1.setViewportView(tabPliki);
        
        
        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 375,Short.MAX_VALUE)// javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                .addContainerGap())
        );
        mainPanel.setVisible(true);
        jScrollPane1.setVisible(true);
        tabPliki.setVisible(true);
        tabPliki.setFillsViewportHeight(true);
        utworzMenu();
        setJMenuBar(menuGlowne);
        add(mainPanel);
        pack();
    }

}
